package com.coderman.dbant;

import com.coderman.dbant.utils.JdbcUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by ${fanchunshuai} on 2017-7-6.
 *
 * @version: V1.0
 * @Desc:
 * @Copyright (c) 2017 58到家-版权所有
 */
public class DBAnt {
    private static String OS = System.getProperty("os.name").toLowerCase();
    public static void main(String[] args) {
        if(args != null && args.length == 6){
            DBAnt dbAnt = new DBAnt();
            try {
                System.out.println(args[0]+"---"+args[1]+"---"+args[2]+"---"+args[3]+"---"+args[4]+"---"+args[5]);
                dbAnt.generateBean(args[0],args[1],args[2],args[3],args[4],args[5]);
            } catch (Exception e) {
                System.out.println("db error............");
            }
        }else{
            System.out.println("args  not match  parameters ............");
        }
    }
    public boolean generateBean(String dbUrl,String dbName,String userName,String password,String tableName,String whereArgs) throws Exception {

        StringBuffer sql1 = new StringBuffer("select * from "+tableName);

        //where 条件可选
        if( !whereArgs.equals("$") ){
            if (whereArgs.contains(",")){
                sql1.append(" where ");
                sql1.append(whereArgs.replace(","," and "));
            }else{
                sql1.append(" where ");
                sql1.append(whereArgs);
            }
        }
        System.out.println("sql = "+sql1);
        JdbcUtils.initDBConnection(dbUrl, dbName, userName, password);
        List<Map<String,Object>> list = JdbcUtils.selectDBList(sql1.toString());
        StringBuffer insertSql = new StringBuffer();
        for (int i  = 0; i < list.size();i ++){
            Map<String,Object> map = list.get(i);
            StringBuffer insertSqlBatch = new StringBuffer("insert into "+tableName);

            StringBuffer columnList = new StringBuffer();
            StringBuffer valueList = new StringBuffer();
            map.entrySet().stream().filter(it -> !it.getValue().equals("") && !it.getValue().equals("null")).forEach(it -> {
                columnList.append(it.getKey() + ",");
                valueList.append("'" + it.getValue() + "',");
            });

            insertSqlBatch.append(" ( ");
            insertSqlBatch.append(columnList.toString().substring(0,columnList.toString().length() - 1));
            insertSqlBatch.append(" ) values (");
            insertSqlBatch.append(valueList.toString().substring(0,valueList.toString().length() - 1));
            insertSqlBatch.append("); \n");
            insertSql.append(insertSqlBatch.toString());
        }

        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("D:\\db_config.txt",
                    false));
            try {
                if (insertSql.toString().length() > 0){
                    out.write(insertSql.toString());
                }
            } finally {
                out.close();
            }
        } catch (IOException e) {
            System.out.println("write file error...........");
            e.printStackTrace();
        }
        return true;
    }
}
